﻿using FreeSql.Internal.Model;
using QntPlatform.Db;
using QntPlatform.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace QntPlatform.Platform
{
   public class MngBase<T> : IMng<T> where T : class, IEntityBase
    {
       public virtual bool Add(T entity)
        {
            return DbManager.GetDb().Insert(entity).ExecuteAffrows() > 0;

        }
        public bool Remove(IConvertible id)
        {
            return DbManager.GetDb().Delete<T>(id).ExecuteAffrows() > 0;
        }
        public bool Edit(T entity)
        {
            return DbManager.GetDb().Update<T>().SetSource(entity).ExecuteAffrows() > 0;
        }
        public T[] GetList(T entity)
        {
            return DbManager.GetDb().Select<T>().ToList().ToArray();
        }
        public T[] GetList(Expression<Func<T,bool>> where,int pageNum,int pageSize,out long total)
        {
            return DbManager.GetDb().Select<T>().Where(where).Count(out total).OrderByPropertyName("Id",false).Page(pageNum,pageSize).ToList().ToArray();
        }
        public T[] GetList(DynamicFilterInfo where, int pageNum, int pageSize)
        {
            return DbManager.GetDb().Select<T>().WhereDynamicFilter(where).Page(pageNum, pageSize).ToList().ToArray();

        }
        public T GetById(IConvertible id)
        {
            return DbManager.GetDb().Select<T>(id).First();
        }
    }
    
    public interface IMng<T>
        where T : class, IEntityBase
    {
           bool Add(T entity) {
         return   DbManager.GetDb().Insert(entity).ExecuteAffrows()>0;
           
        }
        public bool Remove(IConvertible id)
        {
            return DbManager.GetDb().Delete<T>(id).ExecuteAffrows() > 0;
        }
        public bool Edit(T entity) {
            return DbManager.GetDb().Update<T>().SetSource(entity).ExecuteAffrows()>0;
        }
        public T[] GetList(T entity) {
            return DbManager.GetDb().Select<T>(entity).ToList().ToArray();
        }
        public T GetById(IConvertible id) {
            return DbManager.GetDb().Select<T>(id).First();
        }
        T[] GetList(Expression<Func<T, bool>> where, int pageNum, int pageSize,out long total);
       
          T[] GetList(DynamicFilterInfo where, int pageNum=1, int pageSize=int.MaxValue);
    }

}
